<?php

namespace App\Exports\Flight;

use App\Services\Flight\FlightSearchService;
use Maatwebsite\Excel\Concerns\FromCollection;

class SevenScheduleExport implements FromCollection
{
    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        //
        $data[] = [
            '日期',
            '航班号',
            '起飞',
            '到达',
            '历史出发准点率',
            '历史到达准点率',
            '出发延误30分钟内百分比',
            '出发延误30分钟-1小时内百分比',
            '出发延误1小时-3小时内百分比',
            '出发延误3小时以上百分比',
            '到达延误30分钟内百分比',
            '到达延误30分钟-1小时内百分比',
            '到达延误1小时-3小时内百分比',
            '到达延误3小时以上百分比',
            '出发平均延误时间',
            '到达平均延误时间',
            '航班取消率',
            '七日准点情况',
            '调时率',
            '近期调时航班，格式为“日期，航班是否取消，航班是否调时，调时时长（计划起飞时间延误时长，复数代表提前，单位：分钟）'
        ];

        $flightDynamicService = new FlightSearchService();
        $interface = resolve('App\Repositories\Contracts\FlightDynamicInterface');
        $whereIn = $flightDynamicService->searchFlightDynamic(request());
        $where[] = ['created_at','>=',date('Y-m-d')];
        $dynamics = $interface->get($where,'*',[],'id','desc','flight_id',$whereIn);
        if ($dynamics->isNotEmpty()) {

            foreach ($dynamics as $key=>$value) {

                //计算7天内延误时间和概率
                $sevenDynamic = json_decode($value['sevenDay_ontime'],true);

                $onTimeDeptNum  = 0;
                $advDeptNum     = 0;
                $delayDeptNum   = 0;
                $onTimeArrNum   = 0;
                $advArrNum      = 0;
                $delayArrNum    = 0;

                $depDelayInThirtyM = 0;
                $depDelayThirtyMToOneH = 0;
                $depDelayOneHToThreeH = 0;
                $depDelayOverThreeH = 0;

                $arrDelayInThirtyM = 0;
                $arrDelayThirtyMToOneH = 0;
                $arrDelayOneHToThreeH = 0;
                $arrDelayOverThreeH = 0;

                $depDelayMinute = 0;
                $arrDelayMinute = 0;
                foreach ($sevenDynamic as $sK=>$sV) {

                    $item = explode('，',$sV);
                    if ($item[2] != 'null' && $item[3] != 'null') {

                        /* if ($item[2] < 0) {
                             $advDeptNum++;
                         }*/
                        if ($item[2] <= 15) {
                            $onTimeDeptNum++;
                        }
                        if ($item[2] > 15) {
                            $delayDeptNum++;
                            $depDelayMinute += $item[2];
                            if ($item[2] <= 30) {

                                $depDelayInThirtyM++;
                            } elseif(30 < $item[2] && $item[2] <= 60 ){

                                $depDelayThirtyMToOneH++;
                            } elseif(60 < $item[2] && $item[2] < 180){

                                $depDelayOneHToThreeH++;
                            } else {
                                $depDelayOverThreeH++;
                            }

                        }

                        /* if ($item[3] < 0) {
                             $advArrNum++;
                         }*/
                        if ($item[3] <= 15) {
                            $onTimeArrNum++;
                        }

                        if ($item[3] > 15) {
                            $delayArrNum++;
                            $arrDelayMinute += $item[3];
                            if ($item[3] <= 30) {

                                $arrDelayInThirtyM++;
                            } elseif(30 < $item[3] && $item[3] <= 60 ){

                                $arrDelayThirtyMToOneH++;
                            } elseif(60 < $item[3] && $item[3] < 180) {

                                $arrDelayOneHToThreeH++;
                            } else {
                                $arrDelayOverThreeH++;
                            }

                        }
                    }

                }
                //总天数
                //$dayNum = $onTimeDeptNum + $advDeptNum + $delayDeptNum;

                //计算调时时间和概率
                $lastChangeFlights = json_decode($value['lastChangeFlights'],true);
                $cancelNum = 0;
                $scheduleChangeNum = 0;
                foreach ($lastChangeFlights as $lK=>$lV){

                    $scheduleItem = explode(',',$lV);
                    if ($scheduleItem[1] == 1) {
                        $cancelNum++;
                    }

                    if ($scheduleItem[2] == 1){
                        $scheduleChangeNum++;
                    }
                }

                $scheduleRate =  count($lastChangeFlights) > 0  ? (sprintf('%.2f',$scheduleChangeNum/count($lastChangeFlights)) * 100 ? : 0) : 0;
                if ($scheduleRate >= 70) {
                    $data[] = [
                        $value->created_at->format('Y-m-d'),
                        $value['flight_code'],
                        $value['dep_code'],
                        $value['arr_code'],
                        (1-sprintf('%.2f',$delayDeptNum/count($sevenDynamic))) * 100 ? : '0',
                        (1-sprintf('%.2f',$delayArrNum/count($sevenDynamic))) * 100 ? : '0',
                        sprintf('%.2f',$depDelayInThirtyM/count($sevenDynamic)) * 100 ? : '0',
                        sprintf('%.2f',$depDelayThirtyMToOneH/count($sevenDynamic)) * 100 ? : '0',
                        sprintf('%.2f',$depDelayOneHToThreeH/count($sevenDynamic)) * 100 ? : '0',
                        sprintf('%.2f',$depDelayOverThreeH/count($sevenDynamic)) * 100 ? : '0',
                        sprintf('%.2f',$arrDelayInThirtyM/count($sevenDynamic)) * 100 ? : '0',
                        sprintf('%.2f',$arrDelayThirtyMToOneH/count($sevenDynamic)) * 100 ? : '0',
                        sprintf('%.2f',$arrDelayOneHToThreeH/count($sevenDynamic)) * 100 ? : '0',
                        sprintf('%.2f',$arrDelayOverThreeH/count($sevenDynamic)) * 100 ? : '0',

                        $delayDeptNum ? intval($depDelayMinute / count($sevenDynamic)) : '0',
                        $delayArrNum ?  intval($arrDelayMinute / count($sevenDynamic)) : '0',
                        count($lastChangeFlights) > 0  ? (sprintf('%.2f',$cancelNum/count($lastChangeFlights)) * 100 ? : '0') : '0',
                        $value['sevenDay_ontime'],
                        count($lastChangeFlights) > 0  ? (sprintf('%.2f',$scheduleChangeNum/count($lastChangeFlights)) * 100 ? : '0') : '0',
                        $value['lastChangeFlights']
                    ];
                }


            }

        }

        return collect($data);
    }
}
